package com.hnucm.cooklife.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hnucm.cooklife.R;
import com.hnucm.cooklife.bean.db.Step;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class EditStepAdapter extends RecyclerView.Adapter<EditStepAdapter.EditStepHolder> {

    private Context mContext;
    private List<Integer> mStepList = new ArrayList<>();

    //自定义接口
    private RecomAdapter.onItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(RecomAdapter.onItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }


    public void setStepList(List<Integer> stepList) {
        mStepList.clear();
        mStepList.addAll(stepList);
        notifyDataSetChanged();
    }

    public EditStepAdapter(Context context, List<Integer> stepList) {
        mContext = context;
        mStepList.addAll(stepList);
    }

    @NonNull
    @Override
    public EditStepHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_edit_step,
                parent, false);
        return new EditStepHolder(view,mOnItemClickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull EditStepHolder holder, int position) {
        holder.mStepNo.setText(
                String.format(mContext.getResources().getString(R.string.detail_step),position+1)
        );
    }

    @Override
    public int getItemCount() {
        return mStepList.size();
    }

    class EditStepHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.stepno)
        TextView mStepNo;

        @BindView(R.id.steppic)
        ImageView mStepPic;

        @BindView(R.id.stepdesc)
        EditText mStepDesc;

        public EditStepHolder(@NonNull View itemView,final RecomAdapter.onItemClickListener onItemClickListener) {
            super(itemView);
            ButterKnife.bind(this,itemView);

            mStepPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPosition = getAdapterPosition();
                    if(adapterPosition != RecyclerView.NO_POSITION){
                        onItemClickListener.onSeleClickListener(mStepPic,adapterPosition);
                    }
                }
            });
        }
    }
}
